/** 商品列表功能模块 */
export const goodModuleMixin = {
	data() {
		return {
			goodList: [],
			goodListPage: 1,
			goodListTotal: 0,
			goodListAfresh: false,
			goodListLoading: true,
			goodListEmpty: false,
			goodListRequestStart: false, // 请求开始
			goodListResetTimer: null,
		}
	},
	onPullDownRefresh() { this.resetgoodList(); },
	onReachBottom() { this.goodListLoading && this.getgoodList(); },
	onLoad() { this.getgoodList(); },
	methods: {
		/** 重新获取商品列表列表 */
		resetgoodList() {
			this.goodListAfresh = true;
			this.goodListLoading = true;
			this.goodListEmpty = false;
			this.goodListRequestStart = false;
			clearTimeout(this.goodListResetTimer);
			this.goodListResetTimer = setTimeout(() => this.getgoodList(), 300);
		},
		/** 获取商品列表列表执行 */
		async getgoodList() {
			if (this.goodListRequestStart) return;
			this.goodListRequestStart = true;
			if (this.goodListAfresh) this.goodListPage = 1;
			try {
				var res = await this.$post('6544dfc8ccf1b', {
					page: this.goodListPage,
					list_rows: 10,
					// 其他参数
					goods_type: 0,
					order_type: 1,
				});
				const _goodList = (res.data.data || []).map((t, i) => {
					return { ...t, animationTime: i / 10 + 0.25 }
				})
				if (this.goodListAfresh) this.goodList = _goodList;
				else this.goodList = this.goodList.concat(_goodList);
				this.goodListPage++;
				this.goodListTotal = res.data.total;
				this.goodListLoading = this.goodList.length < this.goodListTotal;
			} catch (e) { this.goodListLoading = false; }
			this.goodListAfresh = false;
			uni.stopPullDownRefresh();
			this.goodListRequestStart = false;
			this.goodListEmpty = !this.goodList.length && !this.goodListLoading;
		},
	}
};